﻿using LY.Manage.Model.EnumType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Manage.Model;
using System.Collections;
using LY.Manage.WebApp.App_Start;
using LY.Manage.Model.SearchParam;
namespace LY.Manage.WebApp.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/
        IBLL.IUserInfoService UserInfoService { get; set; }
        IBLL.Isys_roleMenuMapService sys_roleMenuMapService { get; set; }
        IBLL.Isys_menuService sys_menuService { get; set; }
        IBLL.Itbl_canshuService CanShuService { get; set; }
      
        public ActionResult Index()
        {
            ViewBag.tezhonggeshu = 2;
            return View();


        }


        public ActionResult HomeLigerUI()
        {

            return View();
        }


        public ActionResult HomePage()
        {
            DevManageParam param = new DevManageParam();

            param.tezhong = "是";

            param.baofeizhuangtai = 1;

            //级别和部门
            int level = (int)LoginUser.Sort;
            param.level = level;
            int depid = (int)LoginUser.DepId;
            param.depid = depid;

            //特种设备提醒 提前35天

           var data3 = CanShuService.LoadEntities(u => u.id == 3).FirstOrDefault();

           int day = int.Parse(data3.value);

            DateTime dt = DateTime.Now.AddDays(day);
            param.operDateStart = dt;

         
            //获取顶部和底部标题
            ViewBag.Yonghu = LoginUser.UName;
            ViewBag.id = LoginUser.ID;
            Bindtitile();
            GetMenus1();
            return View();
        }

        public ActionResult NewHomePage()
        {
            Bindtitile();
            GetMenus1();
            return View();
        }

        public ActionResult Main()
        {
          
            return View();
        }


        /// <summary>
        /// 绑定头底标题
        /// </summary>
        public void Bindtitile()
        {

            var data = CanShuService.LoadEntities(u => u.id == 1).FirstOrDefault();
            ViewBag.HeadTitle = data.value;

            var data2 = CanShuService.LoadEntities(u => u.id == 2).FirstOrDefault();
            ViewBag.FootTitile = data2.value;



        }
        public ActionResult GetMenus1()
        {
           
            // 1、按照用户--角色--权限 这条线找找用户的登录的权限放在集合中
            //获取用户登录的信息，因为序列化的时候没有序列号导航属性
            var userInfo = UserInfoService.LoadEntities(
                u => u.ID == LoginUser.ID).FirstOrDefault();
            //获取登录用户的角色
            var userRoleInfo = userInfo.RoleInfo;

            List<MenuInfo> listMenus = new List<MenuInfo>();
            ArrayList alMenu = new ArrayList();
            foreach (var info in userRoleInfo)
            {
                //获取角色对应的菜单
                List<sys_roleMenuMap> rolemenuList = sys_roleMenuMapService.LoadEntities(u => u.RoleId == info.ID).ToList();
                foreach (var plitem in rolemenuList)
                {
                  //  string menucodezhi = plitem.MenuCode;


                    //获取菜单的详细信息绑定
                    List<sys_menu> menulist = sys_menuService.LoadEntities(u => u.MenuCode == plitem.MenuCode).OrderBy(u => u.MenuSeq).ToList();
                   // List<sys_menu> menulist = sys_menuService.LoadEntities(u => u.MenuCode == plitem.MenuCode).ToList();
                    foreach (var menu in menulist)
                    {

                        if (!alMenu.Contains(menu.Id))
                        {
                            alMenu.Add(menu.Id);
                            MenuInfo menuInfo = new MenuInfo();
                            menuInfo.Id = menu.Id;
                            menuInfo.MenuName = menu.MenuName;
                            menuInfo.URL = menu.IconURL;
                            menuInfo.IconClass = menu.IconClass;
                            menuInfo.ParentCode = menu.ParentCode;
                            menuInfo.MenuCode = menu.MenuCode;
                            listMenus.Add(menuInfo);
                        }
                        ViewBag.AllActionList = listMenus;
                    }

                  
                   
                }
           
            }


          return null;




        }
        /// <summary>
        /// 过滤用户权限
        /// 1、按照用户--角色--权限 这条线找找用户的登录的权限放在集合中
        /// 2、按照用户--权限  这条线找出用户的权限 放在一个集合中
        /// 3、将上面的两个集合合并成一个集合
        /// 4、把禁止的权限从总得集合中清除
        /// 5、将总得集合中的重复权限清除
        /// 6、把过滤好的菜单权限生成Json返回
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMenus()
        {
            return null;
            //// 1、按照用户--角色--权限 这条线找找用户的登录的权限放在集合中
            ////获取用户登录的信息，因为序列化的时候没有序列号导航属性
            //var userInfo = UserInfoService.LoadEntities(
            //    u => u.ID == LoginUser.ID).FirstOrDefault();
            ////获取登录用户的角色
            //var userRoleInfo = userInfo.RoleInfo;
            ////根据登录用户的角色获取相应的菜单权限
            //short actionTypeEnum = (short)ActionTypeEnum.MenumActionType;

            //var loginUserMenuActions = (from r in userRoleInfo
            //                            from a in r.ActionInfo
            //                            where a.ActionTypeEnum == actionTypeEnum
            //                            select a).ToList();


            ////2、按照用户--权限  这条线找出用户的权限 放在一个集合中

            //var userActionMenuAction = from a in userInfo.R_UserInfo_ActionInfo
            //                           select a.ActionInfo;

            //var userMenuAction = (from a in userActionMenuAction
            //                      where a.ActionTypeEnum == actionTypeEnum
            //                      select a).ToList();

            ////3、将上面的两个集合合并成一个集合
            //loginUserMenuActions.AddRange(userMenuAction);
            ////4、把禁止的权限从总得集合中清除
            //var forbidActions = (from a in userInfo.R_UserInfo_ActionInfo
            //                     where a.IsPass == false
            //                     select a.ActionInfoID).ToList();

            //var loginUserAllowActions = loginUserMenuActions.Where(a => !forbidActions.Contains(a.ID));
            ////5、将总得集合中的重复权限清除
            //var lastLoginUserActions = loginUserAllowActions.Distinct(new LY.Manage.Model.EqualityComparer());

            ////6、把过滤好的菜单权限生成Json返回
            //var temp = from a in lastLoginUserActions
            //           select new
            //           {
            //               a.Url,
            //               a.MenuIcon,
            //               a.ActionInfoName
            //           };
            //return Json(temp, JsonRequestBehavior.AllowGet);

        }

        //特种设备


        public class DataItem
        {
            /// <summary>
            /// 
            /// </summary>
            public int id { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string username { get; set; }
            /// <summary>
            /// 女
            /// </summary>
            public string sex { get; set; }
            /// <summary>
            /// 城市-0
            /// </summary>
            public string city { get; set; }
            /// <summary>
            /// 签名-0
            /// </summary>
            public string sign { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int experience { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int logins { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int wealth { get; set; }
            /// <summary>
            /// 作家
            /// </summary>
            public string classify { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int score { get; set; }
        }

        public class Root
        {
            /// <summary>
            /// 
            /// </summary>
            public int code { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public string msg { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public int count { get; set; }
            /// <summary>
            /// 
            /// </summary>
            public List<DataItem> data { get; set; }
        }

        public ActionResult GetDataIndex()
        {

            string result = "";

            Root root = new Root()
            {
                code = 0,
                count = 1000,
                msg = "",
                data = new List<DataItem>() {                       
                        new DataItem(){
                            id =1,
                            username="user-0",
                            sex="女",
                            city="城市0",
                            sign="签名",
                            experience=255,
                            logins=24,
                            wealth=82830700,
                            classify="作家",
                            score=57
                        },
                        new DataItem(){
                            id =2,
                            username="user-2",
                            sex="女",
                            city="城市2",
                            sign="签名",
                            experience=255,
                            logins=24,
                            wealth=82830700,
                            classify="作家",
                            score=57
                        }


                    }

            };
            return Json(root, JsonRequestBehavior.AllowGet);
            result = Newtonsoft.Json.JsonConvert.SerializeObject(root);
            return Content(result);

        }

    }
}
